<?php
/**
*  @author  :  邝达峰
*  @date    :  2017.07.12
*  session对象简单封装类
* 
*/
class Session
{
	/**
	 * session id
	 *
	 * @var string
	 * @access protected
	 */
	protected  $sessionID;
	/**
	 * Session空间
	 *
	 * @var string
	 */

	protected $session_space = '';

	/**
	 * 构造函数
	 *
	 * @param string $message	错误提示信息
	 * @param int	 $code		错误代码号
	 * @param string  $sql      出错的sql
	 */
	public function __construct($session_space='')
	{
		session_start();
		//@header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()-86400) . " GMT");
		$this->session_space = $session_space;
	}

	/**
	 * 返回session ID
	 */
	public function getSessionID()
	{
		return $this->sessionID;
	}

	/**
	 * 写入个人session
	 * @param string|array $var
	 * @param string $value
	 * @return void
	 */

	public function writeSession($var, $value = null)
	{
		if (is_array($var)){
			foreach ($var as $key => $value){
				if ($key != ''){
					if (!empty($this->session_space)){
						$_SESSION[$this->session_space][$key] = $value;

					}
					else {
						$_SESSION[$key] = $value;
					}
				}
			}
		}
		else
		{
			if ($var != ''){
				if (!empty($this->session_space)){
					$_SESSION[$this->session_space][$var] = $value;
				}
				else {
					$_SESSION[$var] = $value;
				}
			}
		}
		//写入登录cookie
		if($_SERVER['HTTP_HOST'] == $_SERVER['SERVER_ADDR']){
			$domain = '';
		}
		else{
			$domain = SITE_COOKIE_DOMAIN;
		}
		$time = time() + 3600 * 6;
		if(is_string($var)){
			setcookie($var, $value, $time, '/',$domain);
		}
	}


	/**
	 * 删除个人用户session记录
	 * 如果$var为空，则删除所有people session
	 * 否则，删除指定$var的people session
	 * @param string $var
	 * @return void
	 */

	public function destroySession($var = '')
	{
		//写入登录cookie
		if($_SERVER['HTTP_HOST'] == $_SERVER['SERVER_ADDR']){
			$domain = '';
		}
		else{
			$domain = SITE_COOKIE_DOMAIN;
		}
		//销毁session和cookie
		if(empty($var)){
			if (!empty($this->session_space)){
				unset($_SESSION[$this->session_space]) ;
			}
			else{
				session_unset();
			}
			//销毁所有cookie
			$arrCookie = $_COOKIE;
			foreach ($arrCookie as $k=>$v){
				//销毁cookie
				setcookie($k, '', -1, '/',$domain);
				//销毁session
				$_SESSION[$k] = '';
				unset($_SESSION[$k]);
			}
			//销毁session
			session_start();
			session_unset();
			session_destroy();
		}
		else {
			if (!empty($this->session_space)){
				unset($_SESSION[$this->session_space][$var]);
			}
			else {
				unset($_SESSION[$var]);
			}
			//销毁cookie
			setcookie($var, '', -1, '/',$domain);
		}
	}

	/**
	 * 读取个人用户session记录
	 * 如果$var为空，则读取所有people session
	 * 否则，读取指定$var的people session
	 * @param string $var
	 * @return array|string
	 */	
	public function getSession($var = '')
	{
		if(empty($var)){
			if (!empty($this->session_space))
			{
				return $_SESSION[$this->session_space] ;
			}
			else
			{
				return $_SESSION;
			}
		}
		else{
			if (!empty($this->session_space)){
				if (isset($_SESSION[$this->session_space][$var])){
					return $_SESSION[$this->session_space][$var]?$_SESSION[$this->session_space][$var]:$_COOKIE[$var];
				}
				else{
					return '';
				}
			}
			else{
				if($var=='uid'){
					return $_COOKIE[$var]?$_COOKIE[$var]:'';
				}else
				{
					return isset($_COOKIE[$var])?$_COOKIE[$var]:$_SESSION[$var];
				}
			}
		}

	}

	/**
	 * 是否设定session值
	 * @param string $var 变量名
	 * @return bool
	 */
	public function issetSession($var)
	{
		if (!empty($this->session_space)){
			return isset($_SESSION[$this->session_space][$var]);
		}
		else{
			return isset($_SESSION[$var]);
		}
	}


	/**
	 * 批量设置设定session值
	 * @param array $sessionData 需要设置的session值
	 * @return bool
	 */
	public function batchSetSession($sessionData)
	{
		if(is_array($sessionData))
		{
			foreach($sessionData as $key => $value)
			{
				unset($_SESSION[$key]);
				$this->writeSession($key,$value);
			}
		}
	}

}
?>